package com.example.vaccine.controller;

import com.example.vaccine.VO.PageVO;
import com.example.vaccine.VO.ResultVO;
import com.example.vaccine.entity.Users;
import com.example.vaccine.form.LoginForm;
import com.example.vaccine.service.UsersService;
import com.example.vaccine.util.ResultVOUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Coisn
 * @since 2025-06-12
 */
@RestController
@RequestMapping("/users")
@Tag(name = "用户管理", description = "用户相关接口，如登录、分页查询、获取核查员列表")
public class UsersController {
    @Autowired
    private UsersService usersService;

    @Operation(summary = "获取核查员列表", description = "查询所有角色为checker的用户列表")
    @GetMapping("/checkers")
    public ResultVO<List<Users>> getCheckerList() {
        List<Users> checkerList = usersService.getCheckers();
        return ResultVOUtil.success(checkerList);
    }

    @Operation(summary = "用户登录", description = "根据手机号、密码和验证码登录系统")
    @PostMapping("/login")
    public ResultVO login(
            @Parameter(description = "登录表单，包含手机号、密码和验证码", required = true)
            @RequestBody LoginForm loginForm,
            HttpServletRequest request // 注入 HttpServletRequest，用于验证码校验
    ) {
        return usersService.login(loginForm, request);
    }


    @Operation(summary = "分页查询用户", description = "根据用户名、角色分页查询用户信息，支持模糊查询")
    @GetMapping("/page")
    public ResultVO<PageVO> getUsers(
            @Parameter(description = "用户名模糊查询", in = ParameterIn.QUERY)
            @RequestParam(defaultValue = "") String name,
            @Parameter(description = "角色筛选", in = ParameterIn.QUERY)
            @RequestParam(defaultValue = "") String role,
            @Parameter(description = "页码，从1开始", in = ParameterIn.QUERY)
            @RequestParam(defaultValue = "1") int page,
            @Parameter(description = "每页大小", in = ParameterIn.QUERY)
            @RequestParam(defaultValue = "5") int size
    ) {
        PageVO pageVO = usersService.getUserPage(name, role, page, size);
        return ResultVOUtil.success(pageVO);
    }

    /**
     * 针对单个任务发送邮箱提醒
     * @param taskId 任务ID
     */
    @PostMapping("/sendEmailReminder")
    public ResultVO sendEmailReminder(@RequestParam("taskId") Integer taskId) {
        try {
            usersService.sendEmailReminderByTaskId(taskId);
            return ResultVOUtil.success("邮件发送成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ResultVOUtil.fail();
        }
    }

    @Operation(summary = "上传头像", description = "上传用户头像并返回访问路径")
    @PostMapping("/uploadAvatar")
    public ResultVO uploadAvatar(
            @RequestParam("file") MultipartFile file,
            @RequestParam("userId") Integer userId
    ) {
        return usersService.handleAvatarUpload(userId, file);
    }


}
